package IdeaGui;

import com.phidgets.PhidgetException;
import com.phidgets.TemperatureSensorPhidget;

import java.io.IOException;
import java.text.DecimalFormat;

/**
 * User: Mike
 * Date: 12/17/11
 */

public class localTemperature implements Temperatures {
    static final long startTime = System.currentTimeMillis();
    TemperatureSensorPhidget tempsensor;
    String columnTempC;
    String columnTempF;
    String ambientTempC;
    String ambientTempF;
    Long time;
    MainGui mainGui;


    public localTemperature() {
//        public localTemperature(MainGui mainGui) {
        this.mainGui = mainGui;
        try {
            tempsensor = new TemperatureSensorPhidget();
            tempsensor.openAny();
            System.out.println("waiting for TemperatureSensor attachment...");
            tempsensor.waitForAttachment();
            tempsensor.setTemperatureChangeTrigger(0, 0.2);
            tempsensor.setThermocoupleType(0, TemperatureSensorPhidget.PHIDGET_TEMPERATURE_SENSOR_J_TYPE);
            System.out.println("captured TemperatureSensor attachment.");
            GetTemps(tempsensor);
        } catch (PhidgetException e2) {
            e2.printStackTrace();
        }
    }

    private void GetTemps(TemperatureSensorPhidget tempsensor) throws PhidgetException {
        final double calibration = 0.3;
        double columnDC = tempsensor.getTemperature(0) - calibration;
        double ambientDC = tempsensor.getAmbientTemperature() - calibration;
        double columnDF = columnDC * 9.0 / 5.0 + 32.0;
        double ambientDF = ambientDC * 9.0 / 5.0 + 32.0;
        DecimalFormat df = new DecimalFormat("###.###");
        columnDC = Double.valueOf(df.format(columnDC));
        ambientDC = Double.valueOf(df.format(ambientDC));
        columnDF = Double.valueOf(df.format(columnDF));
        ambientDF = Double.valueOf(df.format(ambientDF));
//        IdeaGui.Data.milliTime = System.currentTimeMillis();
        columnTempC = String.valueOf(columnDC);
        columnTempF = String.valueOf(columnDF);
        ambientTempC = String.valueOf(ambientDC);
        ambientTempF = String.valueOf(ambientDF);
    }

    public void updateTemps() {
        // not used here. Used in TempClient
    }

    public String getColumnTempC() {
        return ambientTempC;
    }

    public String getColumnTempF() {
        return columnTempF;
    }

    public String getAmbientTempC() {
        return ambientTempC;
    }

    public String getAmbientTempF() {
        return ambientTempF;
    }

    public Long getTime() {
        return time;
    }

    // below is taken from the class: MainGui.java.... this is how we used to get the temps when reading the data in from a local thermocouple
    // the implementation of this class hasn't been tested.... i don't expect to plug in the thermocouple locally

//    private void temperature_orig() {
//        ensureEventThread();
//        Runnable readRun = new Runnable() {
//            public void run() {
//                long startTime = System.currentTimeMillis();
//                long currentTime;
//                TemperatureSensorPhidget tempsensor;
//                try {
//                    tempsensor = new TemperatureSensorPhidget();
//                    tempsensor.openAny();
//                    System.out.println("waiting for TemperatureSensor attachment...");
//                    tempsensor.waitForAttachment();
//                    tempsensor.setTemperatureChangeTrigger(0, 0.2);
//                    tempsensor.setThermocoupleType(0, TemperatureSensorPhidget.PHIDGET_TEMPERATURE_SENSOR_J_TYPE);
//                    System.out.println("captured TemperatureSensor attachment.");
//                    while (IdeaGui.Data.programRunning) {
//                        currentTime = System.currentTimeMillis();
//                        IdeaGui.Data.timeS = String.valueOf(((currentTime - startTime) / 1000.0));
//                        GetTemps(tempsensor, startTime);
//                        try {
//                            Thread.sleep((long) 1000);            // sleep for X ms
//                        } catch (InterruptedException e) {
//                            int unableToSleep = 1;
//                        }
//                    }
//                } catch (PhidgetException e2) {
//                    e2.printStackTrace();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//                System.out.println("No longer measureing temperature ------------------------------------");
//            }
//        };
//        tempThread = new Thread(readRun);
//        tempThread.start();
//    }
//
//    public void GetTemps_orig(TemperatureSensorPhidget tempsensor, Long startTime) throws PhidgetException, IOException {
//        final double calibration = 0.3;
//        double columnDC = tempsensor.getTemperature(0) - calibration;
//        double ambientDC = tempsensor.getAmbientTemperature() - calibration;
//        double columnDF = columnDC * 9 / 5 + 32;
//        double ambientDF = ambientDC * 9 / 5 + 32;
//        DecimalFormat df = new DecimalFormat("###.###");
//        columnDC = Double.valueOf(df.format(columnDC));
//        ambientDC = Double.valueOf(df.format(ambientDC));
//        columnDF = Double.valueOf(df.format(columnDF));
//        ambientDF = Double.valueOf(df.format(ambientDF));
//        double runningTime = (1.0 / 1000.0) * (1.0 / 60.0) * (System.currentTimeMillis() - startTime);
//        IdeaGui.Data.milliTime = System.currentTimeMillis();
//        IdeaGui.Data.timeS = String.valueOf(runningTime);
//        double tempChange = columnDC - lastWrittenTemp;
//        if (Math.abs(tempChange) >= minTempChangeForWrite ||
//                lastWrittenBoilTemp.contentEquals(Data.boilTempS) ||               // != Data.boilTempS ||
//                lastWrittenBottleNum.contentEquals(Data.bottleS) ||                // != Data.bottleS ||
//                lastWrittenAbv.contentEquals(Data.corrAbvS)                        // != Data.corrAbvS
//                ) {
//            lastWrittenTemp = columnDC;
//            lastWrittenBoilTemp = boilerSC;
//            lastWrittenBottleNum = bottleNumS;
//            lastWrittenAbv = corrAbvS;
//            Writer.WriteData(writeOut, runningTime);
//        }
//        setTempTextSafe();
//    }


}
